package com.haihui.platform.controller.sys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.haihui.platform.controller.BaseController;
import com.haihui.platform.entity.DustBaseModel;
import com.haihui.platform.entity.Role;
import com.haihui.platform.entity.SysAdUserInfo;
import com.haihui.platform.service.sys.SysAdUserService;
import com.haihui.platform.utils.PrimaryKeyGen;
import com.haihui.platform.utils.TripleDES;

/*
 * 系统人员
 */
@Controller
@RequestMapping("/sysuser")
public class SysAdUserController extends BaseController{

	private static final Logger LOG = Logger.getLogger(SysAdUserController.class);
	
	@Autowired
	private SysAdUserService sysAdUserService;
	
	/**
	 * 登录
	 * @param user
	 */
	@RequestMapping(value="/login")
	public void login(SysAdUserInfo user){
		try{
			user.setAduserPwd(TripleDES.getCiphert(user.getAduserPwd()));
			SysAdUserInfo sysUser = sysAdUserService.login(user);
			if(sysUser != null){
				request.getSession().setAttribute("user", sysUser);
				this.responseWriter(this.getCommonJsonString(true, "查询成功", sysUser));
			}else{
				this.responseWriter(this.getCommonJsonString(false, "用户名或密码错误"));
			}
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "登录失败"));
		}
	}
	
	/**
	 * 系统用户列表
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public void list(SysAdUserInfo user){
		
		try{
			//获取用户列表条数
			int Total =sysAdUserService.getUserCount(user);
			//系统用户列表
			List<SysAdUserInfo> list = sysAdUserService.list(user);
			//获返回取json
			this.responseWriter(this.getCommonJsonPage(Total, list));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);		
		}	
	}
	
	/**
	 * 新建用户
	 * @param user
	 */
	@RequestMapping(value="/add")
	public void add(@RequestBody SysAdUserInfo user){
		try{
			user.setAduserId(PrimaryKeyGen.getKey());
			user.setAduserPwd(TripleDES.getCiphert("88888888"));
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        	String time = df.format(new Date());
			user.setCreateTime(time);
			user.setCreator("3713");
			int count = sysAdUserService.add(user);
			  if(count > 0){
	                this.responseWriter(this.getCommonJsonString(true,"创建成功"));
	            }else{
	                this.responseWriter(this.getCommonJsonString(false,"创建失败"));
	            }
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			//this.responseWriter(this.getCommonJsonString(false, "创建失败"));
		}
	}
	
	/**
     * 用户详细信息
     * @param id
     */
    @RequestMapping(value = "/detail")
    public void detail(long id){
        try{
        	SysAdUserInfo model =  new SysAdUserInfo();
            model.setAduserId(id);
            SysAdUserInfo result = sysAdUserService.detail(model);
            if(result != null){
                this.responseWriter(this.getCommonJsonString(true,"",result));
            }else{
                this.responseWriter(this.getCommonJsonString(false,""));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    
    /**
     * 判断用户名是否存在
     * @param id
     */
    @RequestMapping(value = "/getUserByName")
    public void getUserByName(String userName){
        try{
        	SysAdUserInfo model =  new SysAdUserInfo();
            model.setAduserName(userName);
            int count = sysAdUserService.getUserByName(model);
            this.responseWriter(this.getCommonJsonString(true, "用户名已存在", count));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
	/**
	 * 更新用户
	 * @param user
	 */
	@RequestMapping(value="/updateUser")
	public void updateUser(@RequestBody SysAdUserInfo user){
		try{
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        	String time = df.format(new Date());
			user.setModifyTime(time);
			user.setModifyor("3713");
			int count = sysAdUserService.updateUser(user);
			if(count > 0){
                this.responseWriter(this.getCommonJsonString(true,"更新成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"更新失败"));
            }
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "创建失败"));
		}
	}
	
	/**
	 * 更新用户
	 * @param user
	 */
	@RequestMapping(value="/deleteUser")
	public void deleteUser(long id){
		try{
			SysAdUserInfo model =  new SysAdUserInfo();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        	String time = df.format(new Date());
        	model.setAduserId(id);
        	model.setModifyTime(time);
        	model.setModifyor("3713");
			int count = sysAdUserService.deleteUser(model);
			if(count > 0){
                this.responseWriter(this.getCommonJsonString(true,"删除成功"));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"删除失败"));
            }
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "删除失败"));
		}
	}
	
	/**
	 * 更新用户
	 * @param user
	 */	
	@RequestMapping(value="/sysUserRoleList")
	public void sysUserRoleList(){
		try{
			
        	List<Role> list =sysAdUserService.sysUserRoleList();
        	if(list.size() > 0){
                this.responseWriter(this.getCommonJsonString(true,"查询成功",list));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"查询失败"));
            }
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "删除失败"));
		}
	}
	
	/**
	 * 更新用户
	 * @param user
	 */	
	@RequestMapping(value="/sysUserDustList")
	public void sysUserDustList(){
		try{
			
        	List<DustBaseModel> list =sysAdUserService.sysUserDustList();
        	if(list.size() > 0){
                this.responseWriter(this.getCommonJsonString(true,"查询成功",list));
            }else{
                this.responseWriter(this.getCommonJsonString(false,"查询失败"));
            }
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "删除失败"));
		}
	}
	
}
